package com.accgm.api.resources.user;

import org.restlet.Request;

import com.accgm.api.db.dao.AccgmUserDao;
import com.accgm.api.db.model.AccgmUserModel;
import com.accgm.api.resources.base.BaseResource;
import com.accgm.api.utils.Encryptstr;

import net.sf.json.JSONObject;

public class UpdateUserPasswordResource extends BaseResource {

	AccgmUserDao accgmUserDao = AccgmUserDao.getInstance();
	@Override
	protected void initParams(Request request) {
		isAuth = false;
	}

	@Override
	protected JSONObject getMethod(JSONObject json) {
		int isBgRequest = getParameter("isBgRequest",0);
		int userId = getParameter("userId",0);
		String password = getParameter("password","");
		if(isBgRequest != 1){
			return failRequest("无权限访问",json);
		}
		AccgmUserModel accgmUser = accgmUserDao.findByPrimaryKey(userId);
		AccgmUserModel updateAccgmUser = new AccgmUserModel();
		updateAccgmUser.setId(accgmUser.getId());
		updateAccgmUser.setPassword(Encryptstr.encrypt(password));
		accgmUserDao.updateAny(updateAccgmUser);
		return succRequest("更新成功",json);
	}

	@Override
	protected JSONObject postMethod(JSONObject json) {
		return this.getMethod(json);
	}

}
